﻿Ext.define("PSI.OutConsignation.ExportReportDetile", {
    setQC: function (qc) {
        this.qc = qc;
    },

    createToolButtons: function () {
        var me = this;
        var p = me.permission;
        var result = [];
        result.push("-");
        result.push({ text: "导出Excel", iconCls: "psi-button-excel", handler: function () { me.onExcel(me.address,me.time) }, scope: me });

        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                window.location.href = "/Report/Index";
            }
        });
        return result;
    },

    createUI: function (address, time) {
        var me = this;
        me.address = address;
        me.time = time;
        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "出口报表详情",
            tbar: me.createToolButtons(),
            items: [ {
                region: "center", xtype: "container", layout: "fit", border: false,
                items: [me.getPlanGrid()]
                }
            ]
        });

      
        me.panel = panel;
        me.refreshPlanGrid(null, address, time)
       
    },

    getMainPanel: function () {
        return this.panel;
    },

    getPlanGrid: function () {
        var me = this;

        if (!me.__PlanGrid) {
            me.__PlanGrid = me.createPlanGrid();
        }

        return me.__PlanGrid;
    },

    createPlanGrid: function () {
        var modelName = "PSIOutPlanModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "planNumber", "address", "cntNumber", "cntType", "takeCntDT", "memo",
                            "planDT", "dispatcher", "truckDT", "truckCompany", "truckNumber",
                            "driver", "driverTel", "dt0Plan", "dt0Driver", "dt1Plan", "dt1Driver", "dt2Plan", "dt2Driver",
                            "dt3Plan", "dt3Driver", "dt4Plan", "dt4Driver", "vsl", "clients", "cntOwner", "status",
                            "loadCntMan", "loadCntManTel", "traceMemo", "ton", "destPort", "goods"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "状态", dataIndex: "status", menuDisabled: true },
                    { header: "出口计划号", dataIndex: "planNumber", menuDisabled: true },
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 230 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true, width: 60 },
                    { header: "货物", dataIndex: "goods", menuDisabled: true },
                    { header: "吨位", dataIndex: "ton", menuDisabled: true },
                    { header: "目的港", dataIndex: "destPort", menuDisabled: true },
                    { header: "装箱时限", dataIndex: "takeCntDT", menuDisabled: true, width: 120 },
                    { header: "备注", dataIndex: "memo", menuDisabled: true },
                    { header: "计划下达时间", dataIndex: "planDT", menuDisabled: true, width: 120 },
                    { header: "调度", dataIndex: "dispatcher", menuDisabled: true },
                    { header: "拖车公司", dataIndex: "truckCompany", menuDisabled: true },
                    { header: "车号", dataIndex: "truckNumber", menuDisabled: true },
                    { header: "司机", dataIndex: "driver", menuDisabled: true },
                    { header: "司机电话", dataIndex: "driverTel", menuDisabled: true },
                    { header: "派车时间", dataIndex: "truckDT", menuDisabled: true, width: 120 },
                    { header: "装箱联系人", dataIndex: "loadCntMan", menuDisabled: true },
                    { header: "装箱人电话", dataIndex: "loadCntManTel", menuDisabled: true },
                      {
                          header: "到装货地", menuDisabled: true,
                          columns: [
                              { header: "调度计划时间", dataIndex: "dt0Plan", menuDisabled: true, width: 120 },
                              { header: "司机报点", dataIndex: "dt0Driver", menuDisabled: true, width: 120 }
                          ]
                      },
                    {
                        header: "装箱开始", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt1Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt1Driver", menuDisabled: true, width: 120 }
                        ]
                    },
                    {
                        header: "装货完成", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt2Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt2Driver", menuDisabled: true, width: 120 }
                        ]
                    },
                    {
                        header: "回港", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt3Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt3Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "完成", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt4Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt4Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "动态备注", dataIndex: "traceMemo", menuDisabled: true
                    }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    refreshPlanGrid: function (selectedId, address, time) {
        var me = this;
        var grid = me.getPlanGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }
        var params = {
            address: address,
            time: time
        };
        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/OutPlan/GetOutPlanListData",
            params: params,
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onRefreshPlanGird: function () {
        this.refreshPlanGrid();
    },

    onExcel: function (address,time) {
        var p = {
            address: address,
            time: time
        };

        var url = "/OutPlan/Excel?&address=" + p.address + "&time=" + p.time;
            
        window.open(url);
    },


    onClearQC: function () {
        Ext.getCmp("editPlanNumberQC").setValue(null);
        Ext.getCmp("editAddressQC").setValue(null);
        Ext.getCmp("editCntNumberQC").setValue(null);
        Ext.getCmp("editDispatcherQC").setValue(null);
        Ext.getCmp("editTruckCompanyQC").setValue(null);
        Ext.getCmp("editTruckNumberQC").setValue(null);
        Ext.getCmp("editDriverQC").setValue(null);
        Ext.getCmp("editDriverTelQC").setValue(null);
        Ext.getCmp("editPlanDT1QC").setValue(null);
        Ext.getCmp("editPlanDT2QC").setValue(null);
        Ext.getCmp("editStatus").setValue(null);
        Ext.getCmp("editConsignationBillNumberQC").setValue(null);
    },

    getStatusSelect: function () {
        var states = Ext.create('Ext.data.Store', {
            fields: ['id', 'name'],
            data: [
                { "id": "0", "name": "  " },
                { "id": "1", "name": "待下达计划" },
                { "id": "2", "name": "带派车" },
                { "id": "3", "name": "已派车" },
                { "id": "4", "name": "运输完成" },
            ]
        });

        return Ext.create('Ext.form.ComboBox', {
            id: "editStatus",
            name: "editStatus",
            margin: "4 5 0 5",
            store: states,
            width: 170,
            displayField: 'name',
            valueField: 'name'
        })
    }

});